#!/usr/bin/env python

import gtk
import gettext
import math
import os
from blend import constants
from blend import mxf
from signals import emoteWindowSignals

gettext.install('blendIT','locale');

class emoteWindow(gtk.Window):

   def __init__(self,blend):
   
      gtk.Window.__init__(self,gtk.WINDOW_TOPLEVEL);
      
      self.sigs = emoteWindowSignals.signals(blend);
      self.blend = blend;
      self.set_title(_("Emoticons"));
      
      self.tooltips = gtk.Tooltips();
      
      self.set_icon_from_file("images/emoticons/smile.png");
      self.set_border_width(2);
      #vbox = gtk.VBox();
      #vbox.pack_start(self.buttonArray());
      #vbox.pack_end(self.moreEmoteArray());
      #vbox.show();
      #self.add(vbox);

   def show(self):
      vbox = gtk.VBox();
      vbox.pack_start(self.buttonArray());
      vbox.pack_end(self.moreEmoteArray());
      vbox.show();
      c = self.get_children()
      if len(c) > 0:
         for i in range(0,len(c)):
            self.remove(c[i]);
      self.add(vbox);      
      gtk.Window.show(self);

   # Custom emoticons in 'cached/'
   def moreEmoteArray(self):
      list = [];
      for root,dirs,files in os.walk('cached'):
         if root == 'cached':
            for i in range(0,len(files)):
               file = files[i];
               d = self.blend.gui.chatWindow.emoticons.createEmoticon(file);
               if d is not False:
                  list.append(d);
         else:
            break;

      # (shortcut,mxfStuff.name,loc)
      rows = int(math.ceil(float(len(list)) / 12));

      if rows == 0:
         return gtk.Label();

      table = gtk.Table(rows=rows,columns=12);
      for r in range(0,rows):
         for c in range(0,12):
            i = r*12+c;
            if i >= len(list):
               break;
            button = gtk.Button();
            img = gtk.Image();
            img.set_from_file(list[i][2]);
            button.set_image(img);
            self.tooltips.set_tip(button,list[i][1] + " - " +
                                  list[i][0],None);

            button.connect("clicked",self.sigs.on_custom,list[i][0],img.get_pixbuf());
            button.show();
            table.attach(button,c,c+1,r,r+1,gtk.FILL);

      table.show();
      table.set_col_spacings(1);
      table.set_row_spacings(1);
      
      return table;


   def buttonArray(self):
      list = constants.C.emotes;
      rows = int(math.ceil(float(len(list)) / 7));
      table = gtk.Table(rows=rows,columns=7);

      for r in range(0,rows):
         for c in range(0,7):
            i = r*7+c;
            if i >= len(list):
               break;

            button = gtk.Button();
            img = gtk.Image();
            img.set_from_file(list[i].image);
            button.set_image(img);
            self.tooltips.set_tip(button,list[i].text + " - " +
                                  list[i].pattern,None);

            button.connect("clicked",self.sigs.on_selected,list[i]);
            button.show();
            table.attach(button,c,c+1,r,r+1,gtk.FILL);

      table.show();
      table.set_col_spacings(1);
      table.set_row_spacings(1);
      
      return table;